﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFromSocketClient
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 客户端socket
        /// </summary>
        private Socket clientSocket;
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            int port = 6679;
            string host = "127.0.0.1";//服务器端ip地址

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(ipe);

            this.Receive();
        }

        /// <summary>
        /// 接受信息
        /// </summary>
        private  void Receive()
        {
            Task.Run(() =>
            {
                try
                {
                    string recStr = "";
                    byte[] recBytes = new byte[4096];
                    int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
                    recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
                    AddMessageBoxAync(recStr);
                }
                catch (Exception)
                {
                    clientSocket.Close();
                    AddMessageBoxAync("断开连接");
                }
            }).ContinueWith((x)=> {
                if (clientSocket.Connected)
                {
                    this.Receive();
                }
            });
        }
        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="message"></param>
        private void AddMessageBoxAync(string message)
        {
            messageBox.Invoke((Action)(() => { AddMessageBox(message); }));
        }

        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="message"></param>
        private void AddMessageBox(string message)
        {
            messageBox.AppendText(message + "\n");
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            clientSocket.Close();
        }

        private void btSend_Click(object sender, EventArgs e)
        {
            //send message
            string sendStr = this.textBox1.Text;
            byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
            if(clientSocket.Connected) //判断是否连接
            {
                clientSocket.Send(sendBytes);
                textBox1.Text = "";
            }
            else
            {

            }
       
        }
    }
}
